package hao.itheima.service.impl;


import hao.itheima.entity.JobOption;
import hao.itheima.entity.StudentOption;
import hao.itheima.mapper.EmpMapper;
import hao.itheima.mapper.StudentMapper;
import hao.itheima.service.ReportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;


@Slf4j
@Service
public class ReportServiceImpl implements ReportService {

    //依赖注入
    @Autowired
    private EmpMapper empMapper;

    @Autowired
    private StudentMapper studentMapper;

    /**
     * 统计各个职位的员工人数
     *
     * @return
     */

    //涉及到多张表, 开事务
    @Override
    public JobOption getEmpJobData() {
        //没有什么逻辑处理, 直接就往下调用了
        //1、调用mapper接口, 获取统计数据
        List<Map<String, Object>> list = empMapper.selectCountEmpJobData(); // 每一条数据就是: map : pos=职位, num=人数
        log.info(list.toString());
        //2.组装结构, 并返回
        //stream: map进行加工, 根据键获取值
        List<Object> jobList = list.stream().map(jobData -> jobData.get("pos")).toList();
        List<Object> jobNumList = list.stream().map(jobData -> jobData.get("num")).toList();

        return new JobOption(jobList, jobNumList);
    }

    /**
     * 统计员工性别人数
     *
     * @return
     */
    @Override
    public List<Map<String, Object>> getEmpGenderData() {
        //没什么逻辑要进行处理
        return empMapper.selectEmpGenderData();
    }

    /**
     * 统计学员的学历信息
     *
     * @return
     */
    @Override
    public List<Map<String, Object>> getStudentDegreeData() {
        //1、直接调用Mapper进行统计结果
        return studentMapper.getStudentDegreeData();
    }

    /**
     * 统计每个班级的人数
     *
     * @return
     */
    @Override
    public StudentOption getStudentCountData() {
        //1、调用Mapper获取数据
        List<Map<String, Object>> list = studentMapper.getStudentCountData();
        //2、组装结果, 并返回. 是用stream流
        List<Object> className = list.stream().map(clazzNameData -> clazzNameData.get("name")).toList();
        List<Object> classCount = list.stream().map(clazzCount -> clazzCount.get("classValues")).toList();
        //3.创建对象封装结果
        return new StudentOption(className, classCount);
    }
}
